package lib.bone.system.module.notify.service.impl;

import cn.hutool.core.lang.Validator;
import lib.bone.common.constant.ResponseCodeConst;
import lib.bone.common.core.domain.PageResultDTO;
import lib.bone.common.core.domain.ResponseDTO;
import lib.bone.common.exception.BusinessException;
import lib.bone.system.module.iojob.domain.IOJob;
import lib.bone.system.module.notify.dao.SysNotifyDAO;
import lib.bone.system.module.notify.domain.SysNotify;
import lib.bone.system.module.notify.service.SysNotifyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Slf4j
@Service
public class SysNotifyServiceImpl implements SysNotifyService {
    @Autowired
    SysNotifyDAO dao;

    @Override
    public PageResultDTO<SysNotify> getAll(Pageable pageable, Example<SysNotify> example) {
        Page<SysNotify> page;
        //是否为空 --
        if(Validator.isEmpty(example)){
            page = dao.findAll(pageable);
        }else {
            page = dao.findAll(example,pageable);
        }
        List<SysNotify> list = page.getContent();
        PageResultDTO<SysNotify> pageResultDTO = new PageResultDTO<>();
        pageResultDTO.setContent(list);
        pageResultDTO.setSize(page.getSize());
        pageResultDTO.setTotalPages(page.getTotalPages());
        pageResultDTO.setNumber(page.getNumber());
        pageResultDTO.setTotalElements(page.getTotalElements());

        return pageResultDTO;
    }

    @Override
    public ResponseDTO<SysNotify> deleteById(Long id) {
        dao.deleteById(id);
        return ResponseDTO.succMsg("删除成功");
    }

    @Override
    public Optional<SysNotify> getById(Long id) {
        return dao.findById(id);
    }

    @Override
    public SysNotify saveOne(SysNotify sysNotify) {
        if (Validator.isNotEmpty(sysNotify.getId())){
            throw new BusinessException(ResponseCodeConst.ERROR_PARAM);
        }
        return dao.save(sysNotify);
    }

    @Override
    public SysNotify updateOne(SysNotify sysNotify) {
        if (Validator.isEmpty(sysNotify.getId())){
            throw new BusinessException(ResponseCodeConst.ERROR_PARAM);
        }
        return dao.save(sysNotify);
    }
}
